package pe.gob.mpfn.model;

import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Entity
@Table(name = "expediente_parte")
@Configurable
@RooJavaBean
@RooToString
@RooJpaActiveRecord(versionField = "", table = "expediente_parte")
@RooDbManaged(automaticallyDelete = true)
public class ExpedienteParte {

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "int_expediente_parte_id", columnDefinition = "INT")
    private Integer intExpedienteParteId;

	public Integer getIntExpedienteParteId() {
        return this.intExpedienteParteId;
    }

	public void setIntExpedienteParteId(Integer id) {
        this.intExpedienteParteId = id;
    }

	@ManyToOne
    @JoinColumn(name = "str_numero_expediente_id", referencedColumnName = "str_numero_expediente_id", nullable = false)
    private Expediente strNumeroExpedienteId;

	@ManyToOne
    @JoinColumn(name = "int_parte_id", referencedColumnName = "int_parte_id", nullable = false)
    private Parte intParteId;

	public Expediente getStrNumeroExpedienteId() {
        return strNumeroExpedienteId;
    }

	public void setStrNumeroExpedienteId(Expediente strNumeroExpedienteId) {
        this.strNumeroExpedienteId = strNumeroExpedienteId;
    }

	public Parte getIntParteId() {
        return intParteId;
    }

	public void setIntParteId(Parte intParteId) {
        this.intParteId = intParteId;
    }

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new ExpedienteParte().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countExpedientePartes() {
        return entityManager().createQuery("SELECT COUNT(o) FROM ExpedienteParte o", Long.class).getSingleResult();
    }

	public static List<ExpedienteParte> findAllExpedientePartes() {
        return entityManager().createQuery("SELECT o FROM ExpedienteParte o", ExpedienteParte.class).getResultList();
    }

	public static ExpedienteParte findExpedienteParte(Integer intExpedienteParteId) {
        if (intExpedienteParteId == null) return null;
        return entityManager().find(ExpedienteParte.class, intExpedienteParteId);
    }

	public static List<ExpedienteParte> findExpedienteParteEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM ExpedienteParte o", ExpedienteParte.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            ExpedienteParte attached = ExpedienteParte.findExpedienteParte(this.intExpedienteParteId);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public ExpedienteParte merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        ExpedienteParte merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }
}
